package cn.xz.gobang.activity

import android.graphics.Color
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import cn.xz.gobang.R
import cn.xz.gobang.adapter.ChessPAdapter
import cn.xz.gobang.base.BaseActivity
import cn.xz.gobang.bean.ChessPData
import cn.xz.gobang.bean.ClickData
import cn.xz.gobang.bean.CommonLib
import cn.xz.gobang.bean.UserData
import cn.xz.gobang.databinding.ActivityRoBotBinding
import cn.xz.gobang.dialog.WinDialog
import cn.xz.gobang.util.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * 机器人模式*/
class RoBotActivity : BaseActivity<ActivityRoBotBinding>(R.layout.activity_ro_bot){

    private var mUserBean: UserData? = null //我的数据
    private var chessPAdapter: ChessPAdapter? = null
    private var hasWin = false //当前是否有赢家(游戏结束)
    private var mType = true //当前是否是黑棋，默认黑棋先下
    private var downType = true //当前谁在下棋  true 我下
    private var mClickData: ArrayList<ClickData> = arrayListOf() //已下棋子集合
    private var mClick: ChessPData? = null //当前下的棋子
    private var mIndex: Int = -1 //当前下的棋子的下标
    private var mDownTimer = 60 //倒计时
    private var timer: Timer? = null //定时器

    override fun initData() {
        //先设置好棋盘
        setP()
        //获取自己的数据
        getMine()
        //显示对战界面
        setPlay()
        //我先下，开启倒计时
        startTime()

    }

    private fun getMine(){
        mDataBind.tvMineName.post {
            //获取用户数据
            GetDataUtils.onUserData(CommonLib.getUserID,object :GetDataUtils.OnReadDataListener{
                override fun success(bean: UserData) {
                    mUserBean = bean
                    //名字
                    mDataBind.tvMineName.post {
                        mDataBind.tvMineName.text = bean.name
                        mDataBind.tvMineDj.text = bean.gameNumber.toString()
                        val mD = GetMyDataUtil.getNum(bean.userpart)
                        mDataBind.ivMineJb.setImageDrawable(mD.userPic)
                        mDataBind.tvMineDw.text = mD.userDW
                        mDataBind.tvMineDw.setTextColor(Color.parseColor(mD.userColor!!))
                    }
                }
                override fun error() {
                    val mD = GetDataUtils.getUserCache()
                    if (mD != null){
                        mUserBean = mD
                        //名字
                        mDataBind.tvMineName.post {
                            mDataBind.tvMineName.text = mD.name
                            mDataBind.tvMineDj.text = mD.gameNumber.toString()
                            val mD = GetMyDataUtil.getNum(mD.userpart)
                            mDataBind.ivMineJb.setImageDrawable(mD.userPic)
                            mDataBind.tvMineDw.text = mD.userDW
                            mDataBind.tvMineDw.setTextColor(Color.parseColor(mD.userColor!!))
                        }
                    }
                }
            })
        }
    }

    private fun setPlay(){

    }

    //开游戏，开始定时
    private fun startTime(){
        timer = Timer()
        val timerTask: TimerTask = object : TimerTask() {
            override fun run() {
                mDownTimer--
                if (downType){
                    showMineTime()
                }else{
                    shoeOtherTime()
                }
                if (mDownTimer < 0){
                    //倒计时结束
                    cancelTime()
                    //当前谁在下棋，下棋者输
                    var type = 0
                    if (!downType){
                        //我在下
                        type = 1
                    }
                    hasWin()
                    WinDialog.newInstance(type,-1).show(this@RoBotActivity.supportFragmentManager,"WinDialog123456")
                }
            }
        }
        timer?.schedule(timerTask, 0, 1000) //开启定时
    }

    private fun cancelTime(){
        if (timer != null){
            timer!!.cancel()
            timer = null
        }
    }

    //显示我方倒计时
    private fun showMineTime(){
        mDataBind.tvMineTime.post {
            if (mDataBind.clMineTime.isGone){
                mDownTimer = 60
                mDataBind.clMineTime.visible()
                mDataBind.viewUnClick.gone()
                mDataBind.clPkTime.gone()
            }
            mDataBind.tvMineTime.text = mDownTimer.toString()
        }
    }

    //显示对方倒计时
    private fun shoeOtherTime(){
        mDataBind.tvPkTime.post {
            if (mDataBind.clMineTime.isVisible){
                mDownTimer = 60
                mDataBind.clMineTime.gone()
                mDataBind.viewUnClick.visible()
                mDataBind.clPkTime.visible()
            }
            mDataBind.tvPkTime.text = mDownTimer.toString()
        }
    }

    override fun initEvent() {
        mDataBind.viewUnClick.setOnClickListener {  }
        //确认按钮
        mDataBind.llcOn.setOnClickListener {
            if (mClick != null && mIndex != -1){
                mType = !mType
                mClickData.add(ClickData(mClick!!.type,mIndex))

                mDataBind.llcClick.gone()

                //每次下完棋将数据存储起来
                ClickWinUtil.to(mClickData, chessPAdapter!!.data,object: ClickWinUtil.OnClickWinListener{
                    override fun success(type: Int) {
                        hasWin()
                        WinDialog.newInstance(type,-1).show(this@RoBotActivity.supportFragmentManager,"WinDialog123456")
                    }
                })

                //人机下
                downType = false
                mDataBind.llcOn.postDelayed({
                    if (!hasWin) {
                        var clickType = 0
                        if (!mType) {
                            clickType = 1
                        }
                        val mm: ClickData? = RoBotUtils.go(0, clickType, mClickData)
                        if (mm != null) {
                            getP(mm)
                        }
                        if (!hasWin) {
                            downType = true
                        }
                    }
                }, 1000)
            }
        }
        //取消按钮
        mDataBind.llcCancel.setOnClickListener {
            val mData = chessPAdapter!!.data
            mData[mIndex].type = -1
            chessPAdapter!!.notifyItemChanged(mIndex)
            mDataBind.viewUnClick.gone()
            mDataBind.llcClick.gone()
        }
        //退出
        mDataBind.llcBack.setOnClickListener {
            finish()
        }
    }

    //棋子上盘
    private fun getP(clickData: ClickData){
        val mData = chessPAdapter!!.data
        mClick = mData[clickData.position]
        if (mClick != null){
            //判断当前选位置是否有棋子,有棋子了，显示音效
            if (mClick!!.type == -1){
                var clickType = 1
                if (mType){
                    clickType = 0
                }
                mClick!!.type = clickType
                chessPAdapter!!.notifyItemChanged(clickData.position)
                mIndex = clickData.position

                //判断输赢
                if (mClick != null && mIndex != -1){
                    mType = !mType
                    mClickData.add(ClickData(mClick!!.type,mIndex))
                    //每次下完棋将数据存储起来
                    ClickWinUtil.to(mClickData, chessPAdapter!!.data,object: ClickWinUtil.OnClickWinListener{
                        override fun success(type: Int) {
                            hasWin()
                            WinDialog.newInstance(type,-1).show(this@RoBotActivity.supportFragmentManager,"WinDialog123456")
                        }
                    })
                }
            }else{
                ToastUtil.show("已有棋子")
            }
        }
    }

    //设置棋盘
    private fun setP(){
        mDataBind.rvQp.post {
            val ms = object : GridLayoutManager(this, 15) {
                //禁止上下滑动
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            mDataBind.rvQp.layoutManager = ms
            chessPAdapter = ChessPAdapter()
            mDataBind.rvQp.adapter = chessPAdapter
            val mChess: ArrayList<ChessPData> = arrayListOf()
            for (i in 1..225){
                mChess.add(ChessPData())
            }
            chessPAdapter?.setList(mChess)
            chessPAdapter?.setOnItemClickListener { adapter, view, position ->
                val mData = chessPAdapter!!.data
                mClick = mData[position]
                if (mClick != null){
                    //判断当前选位置是否有棋子,有棋子了，显示音效
                    if (mClick!!.type == -1){
                        var clickType = 1
                        if (mType){
                            clickType = 0
                        }
                        mClick!!.type = clickType
                        chessPAdapter!!.notifyItemChanged(position)
                        mIndex = position
                        //显示是否落棋，禁止点击
                        mDataBind.viewUnClick.visible()
                        mDataBind.llcClick.visible()
                    }else{
                        ToastUtil.show("已有棋子")
                    }
                }
            }
        }
    }

    //有胜利方
    private fun hasWin(){
        hasWin = true
        mDataBind.viewUnClick.post {
            //有获胜方之后显示蒙层，禁止点击
            mDataBind.viewUnClick.visible()
            //取消定时器
            cancelTime()
            //隐藏倒计时
            mDataBind.clMineTime.gone()
            mDataBind.clPkTime.gone()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cancelTime()
    }

}